package com.gmom.tako.db;

import java.util.ArrayList;
import java.util.List;

import com.gmom.tako.model.Rss;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class articleDao {
	private static final String TABLE_NAME = "article";
	private static final String COLUMN_ID = "_id";
	private static final String COLUMN_URL_ID = "url_id";
	private static final String COLUMN_TITLE = "title";
	private static final String COLUMN_DETAIL = "detail";
	private static final String COLUMN_DATE = "date";
	private static final String COLUMN_UID = "uid";
	private static final String COLUMN_LINK = "link";

	private static final String[] COLUMNS = { COLUMN_ID, COLUMN_URL_ID,
			COLUMN_TITLE, COLUMN_DETAIL, COLUMN_DATE, COLUMN_UID, COLUMN_LINK };

	private SQLiteDatabase db;

	public articleDao(SQLiteDatabase db) {
		this.db = db;
	}

	public long insert(Rss rss) {
		return db.insert(TABLE_NAME, null, toContentValues(rss));
	}

	public int update(Rss rss) {
		String whereClause = COLUMN_ID + "=" + rss.get_id();
		return db.update(TABLE_NAME, toContentValues(rss), whereClause, null);
	}

	public List<Rss> findAll() {
		List<Rss> urlList = new ArrayList<Rss>();

		Cursor c = db.query(TABLE_NAME, COLUMNS, null, null, null, null, null);
		c.moveToFirst();
		while (c.moveToNext()) {
			urlList.add(toRss(c));
		}
		return urlList;
	}

	public List<Rss> findByUrlId(int url_id) {
		List<Rss> rssList = new ArrayList<Rss>();

		Cursor c = db.query(TABLE_NAME, COLUMNS, COLUMN_URL_ID + "=" + url_id,
				null, null, null, null);
		c.moveToFirst();
		while (c.moveToNext()) {
			rssList.add(toRss(c));
		}
		return rssList;
	}

	public Rss findByUid(int uid) {
		String selection = COLUMN_UID + "=" + uid;
		Cursor c = db.query(TABLE_NAME, COLUMNS, selection, null, null, null,
				null);
		c.moveToFirst();
		if (c != null && c.getCount() > 0) {
			return toRss(c);
		}
		return null;
	}

	public int delete(int urlId) {
		return db.delete(TABLE_NAME, COLUMN_ID + "=" + urlId, null);
	}

	private Rss toRss(Cursor c) {
		Rss rss = new Rss();

		if (c != null) {
			rss.set_id(c.getInt(c.getColumnIndex(COLUMN_ID)));
			rss.setUrl_id(c.getInt(c.getColumnIndex(COLUMN_URL_ID)));
			rss.setTitle(c.getString(c.getColumnIndex(COLUMN_TITLE)));
			rss.setDetail(c.getString(c.getColumnIndex(COLUMN_DETAIL)));
			rss.setDate(c.getString(c.getColumnIndex(COLUMN_DATE)));
			rss.setUid(c.getInt(c.getColumnIndex(COLUMN_UID)));
			rss.setLink(c.getString(c.getColumnIndex(COLUMN_LINK)));
		}
		return rss;
	}

	private ContentValues toContentValues(Rss rss) {

		ContentValues values = new ContentValues();
		values.put(COLUMN_URL_ID, rss.getUrl_id());
		values.put(COLUMN_TITLE, rss.getTitle());
		values.put(COLUMN_DETAIL, rss.getDetail());
		values.put(COLUMN_DATE, rss.getDate());
		values.put(COLUMN_UID, rss.getUid());
		values.put(COLUMN_LINK, rss.getLink());

		return values;
	}

}
